package com.yike.user.controller;

import com.yike.common.api.Add;
import com.yike.common.api.ApiResult;
import com.yike.common.api.Update;
import com.yike.common.base.BaseController;
import com.yike.common.enums.OperationLogType;
import com.yike.common.log.Module;
import com.yike.common.log.OperationLog;
import com.yike.common.pagination.Paging;
import com.yike.user.param.CustomerPageParam;
import com.yike.user.param.CustomerParam;
import com.yike.user.service.PlatformCustomerService;
import com.yike.user.vo.CustomerVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 平台客户 控制器
 *
 * @author Sakura
 * @since 2024-10-31
 */
@Slf4j
@RestController
@RequestMapping("/platform/customer")
@Module("user")
@Api(value = "平台客户API", tags = {"平台客户管理"})
public class PlatformCustomerController extends BaseController {

    @Autowired
    private PlatformCustomerService platformCustomerService;

    /**
     * 添加客户
     */
    @PostMapping("/add")
    @OperationLog(name = "添加客户", type = OperationLogType.ADD)
    @ApiOperation(value = "添加客户", response = ApiResult.class)
    public ApiResult<Boolean> addCustomer(@Validated(Add.class) @RequestBody CustomerParam customerParam) throws Exception {
        boolean flag = platformCustomerService.saveCustomer(customerParam);
        return ApiResult.result(flag);
    }

    /**
     * 修改客户
     */
    @PostMapping("/update")
    @OperationLog(name = "修改客户", type = OperationLogType.UPDATE)
    @ApiOperation(value = "修改客户", response = ApiResult.class)
    public ApiResult<Boolean> updateCustomer(@Validated(Update.class) @RequestBody CustomerParam customerParam) throws Exception {
        boolean flag = platformCustomerService.updateCustomer(customerParam);
        return ApiResult.result(flag);
    }

    /**
     * 删除客户
     */
    @PostMapping("/delete/{id}")
    @OperationLog(name = "删除客户", type = OperationLogType.DELETE)
    @ApiOperation(value = "删除客户", response = ApiResult.class)
    public ApiResult<Boolean> deleteCustomer(@PathVariable("id") Long id) throws Exception {
        boolean flag = platformCustomerService.deleteCustomer(id);
        return ApiResult.result(flag);
    }

    @PostMapping("/batchDel")
    @OperationLog(name = "批量删除客户", type = OperationLogType.UPDATE)
    @ApiOperation(value = "批量删除客户", response = ApiResult.class)
    public ApiResult<Boolean> batchDel(@Validated(Update.class) @RequestBody List<Long> ids) throws Exception {
        boolean flag = platformCustomerService.batchDel(ids);
        return ApiResult.result(flag);
    }

    /**
     * 客户详情
     */
    @GetMapping("/info/{id}")
    @OperationLog(name = "客户详情", type = OperationLogType.INFO)
    @ApiOperation(value = "客户详情", response = CustomerVo.class)
    public ApiResult<CustomerVo> getCustomer(@PathVariable("id") Long id) throws Exception {
        CustomerVo customerVo = platformCustomerService.getCustomer(id);
        return ApiResult.ok(customerVo);
    }

    @PostMapping("/getPageList")
    //@OperationLog(name = "客户分页列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "客户分页列表", response = CustomerVo.class)
    public ApiResult<Paging<CustomerVo>> getCustomerPageList(@Validated @RequestBody CustomerPageParam customerPageParam) throws Exception {
        Paging<CustomerVo> paging = platformCustomerService.getCustomerPageList(customerPageParam);
        return ApiResult.ok(paging);
    }


}

